/*
* Basic tools for cross-domain javascript interactions.
* Author: Hakimov Irik, hakimovis@yandex.ru
*/

var CDT_DEFAULT_SCRIPT_ID = 'some_script';

// Loads CSS file
function cdt_load_css(css_url) {
    var css = document.createElement('link');
    css.rel = "stylesheet";
    css.type = "text/css";
    css.href = css_url;
    css.media = "screen, projection";
    document.getElementsByTagName("head")[0].appendChild(css);
};

// Reloads JS script, can be used to perform repetitive queries of same type
function cdt_update_js(js_url, js_id) {
    if (js_id == undefined) js_id = CDT_DEFAULT_SCRIPT_ID;
    var e = document.getElementById(js_id); 
    if (e != undefined) e.parentNode.removeChild(e);
    cdt_load_js(js_url, js_id);
};

// Loads JS file
function cdt_load_js(js_url, js_id) {
    if (js_id == undefined) js_id = CDT_DEFAULT_SCRIPT_ID;
    var script = document.createElement('script');
    script.id = js_id;
    script.type = "text/javascript";
    uniq_string = js_id + cdt_uniq_string();

    if (js_url.indexOf("?") > 0) delim = '&'; else delim = '?';
    script.src = js_url + delim + uniq_string;
    document.getElementsByTagName("head")[0].appendChild(script);
};

// Unique string, used to prevent caching on client or server side
function cdt_uniq_string() { return Math.random().toString().replace(/\./g,""); };

console.log('Crossdomain tools loaded');
// Callback like onLoad, runs when script is loaded.
if (typeof(cdt_tools_loaded) == 'undefined') { cdt_tools_loaded = function() {}; };
cdt_tools_loaded();
